package com.thx.information;

import com.thx.auth.User;
import com.thx.common.util.ThreadLocalContext;

import org.apache.commons.lang3.math.NumberUtils;

import java.util.Iterator;
import java.util.List;

public class HqlUtils {

  public static void addEnterpriseId(StringBuffer hql, List<Object> objs) {
//    User user = (User) ThreadLocalContext.get("user");
//    Long enterpriseId = NumberUtils.toLong(user.getTenantId());
    Long enterpriseId = NumberUtils.toLong(ThreadLocalContext.get("enterpriseId").toString());
    hql.append(" enterpriseId = ? ");
    objs.add(enterpriseId);
  }

  public static void personnelCompetenceHql(StringBuffer hql, List<Object> objs) {
    Long personnelId = NumberUtils.toLong(ThreadLocalContext.get("personnelId").toString());
    Long deptId = NumberUtils.toLong(ThreadLocalContext.get("deptId").toString());
    Long postId = NumberUtils.toLong(ThreadLocalContext.get("postId").toString());
    hql.append(" ( ");
    hql.append(" (perBound is null and deptBound is null and roleBound is null) ");
    if (personnelId != null && personnelId != 0L) {
      hql.append(" or ");
      hql.append("(perBound is not null and  perBound like ? )");
      objs.add("%" + personnelId + "%");
      hql.append(" or ");
      hql.append("(createUserId is not null and createUserId = ?) ");
      objs.add(personnelId);
    }
    if (deptId != null && deptId != 0L) {
      hql.append(" or ");
      hql.append("(deptBound is not null and  deptBound like ? )");
      objs.add("%" + deptId + "%");
    }
    if (postId != null && postId != 0L) {
      hql.append(" or ");
      hql.append("(roleBound is not null and  roleBound like ? )");
      objs.add("%" + postId + "%");
    }
    hql.append(" ) ");
  }
  
  public static Long getEnterpriseId() {
    User user = (User) ThreadLocalContext.get("user");
    Long enterpriseId = NumberUtils.toLong(user.getTenantId());
    return enterpriseId;
  }

}
